import os.path

import requests
from bs4 import BeautifulSoup


def download_file(url):
    print('正在下载网页：%s ...' % url)
    res = requests.get(url)
    soup = BeautifulSoup(res.text, 'html.parser')

    img_list = soup.select('.vpic_wrap img')
    print('找到图片', img_list)
    if len(img_list) == 0:
        print('没有找到图片')
    else:
        # 找到资源，开始写入
        for img_info in img_list:
            file_url = img_info.get('bpic')
            write_file(file_url, 1)

    video_list = soup.select('.threadlist_video a')
    if len(video_list) == 0:
        print('没有找到视频')
    else:
        for video_info in video_list:
            file_url = video_info.get('data-video')
            write_file(file_url, 2)

    next_link = soup.select('#frs_list_pager .next')
    if len(next_link) == 0:
        print('没有下一页')
    else:
        next_url = next_link[0].get('href')
        download_file('https:' + next_url)


# 写入文件
def write_file(file_url, file_type):
    res = requests.get(file_url)
    res.raise_for_status()
    file_folder = 'tieba\\other'
    if file_type == 1:
        file_folder = 'tieba\\jpg'
    elif file_type == 2:
        file_folder = 'tieba\\mp4'

    folder = os.path.exists(file_folder)
    if not folder:
        os.makedirs(file_folder)

    file_name = os.path.basename(file_url)
    str_index = file_name.find('?')
    if str_index > 0:
        file_name = file_name[0:str_index]
    file_path = os.path.join(file_folder, file_name)
    print('正在写入资源文件：', file_path)
    image_file = open(file_path, 'wb')
    for chunk in res.iter_content(chunk_size=100000):  # chunk_size：每次迭代返回的数据块大小，默认为128字节
        image_file.write(chunk)
    image_file.close()
    print('写入完成！')


if __name__ == '__main__':
    web_url = 'https://tieba.baidu.com/f?kw=%E5%AD%99%E7%AC%91%E5%B7%9D'
    download_file(web_url)
